package swings.labels;

import swings.buttons.AddParamButton;
import swings.panels.HeadersPanel;
import swings.panels.ParamsPanel;
import swings.panels.TemplatePanel;
import swings.panels.WorkPanel;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class AddParamLabel extends JLabel {

    private ParamsPanel paramsPanel;
    private HeadersPanel headersPanel;
    private TemplatePanel templatePanel;

    private int y;
    private int width = WorkPanel.width-20;

    public AddParamLabel(int y, ParamsPanel paramsPanel) {
        this.y = y;
        this.paramsPanel = paramsPanel;
        AddParamButton addParamButton = new AddParamButton();
        addParamButton.addActionListener(new addParam());
        this.add(addParamButton);
        this.setLayout(null);
        this.setIcon(ImageUtils.common);
        this.setVerticalTextPosition(JLabel.CENTER);
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setBounds(5, Math.min(y, WorkPanel.height / 2 - 110), width, 30);
    }

    public AddParamLabel(int y, HeadersPanel headersPanel) {
        this.y = y;
        this.headersPanel = headersPanel;
        AddParamButton addParamButton = new AddParamButton();
        addParamButton.addActionListener(new addHeader());
        this.add(addParamButton);
        this.setLayout(null);
        this.setIcon(ImageUtils.common);
        this.setVerticalTextPosition(JLabel.CENTER);
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setBounds(5, Math.min(y, WorkPanel.height / 2 - 110), width, 30);
    }

    public AddParamLabel(int y, TemplatePanel templatePanel) {
        this.y = y;
        this.templatePanel = templatePanel;
        AddParamButton addParamButton = new AddParamButton();
        addParamButton.addActionListener(new addTemplate());
        this.add(addParamButton);
        this.setLayout(null);
        this.setIcon(ImageUtils.common);
        this.setVerticalTextPosition(JLabel.CENTER);
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setBounds(5, Math.min(y, WorkPanel.height / 2 - 110), width, 30);
    }

    private class addParam implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                paramsPanel.newParamLabel();
                change();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    private class addHeader implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                headersPanel.newParamLabel();
                change();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    private class addTemplate implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                templatePanel.newParamLabel();
                change();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    private void change(){
        if (y <= WorkPanel.height/2-140){
            y += 30;
            this.setBounds(5,y,width,30);
            this.invalidate();
            this.validate();
            this.repaint();
        }
    }

}

